#This R script replicates the survey-based results of Gessler, Toth, and Wachs "No country for asylum seekers? How short-termexposure to refugees influences attitudes and votingbehavior in Hungary".
require(stargazer)
require(jtools)
require(ggplot2)
require(ggpubr)

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

#read in the data
df<-read.csv('survey_data.csv')


#Table 3, logistic regression interaction models
controls <- 'within_25km_of_border+at_least_hs_degree+precarious_economic_situation+male+as.factor(region)'
key_terms <-'+treatment+voteright+treatment:voteright'
m1<-glm(paste('no_refugees~',controls,key_terms),
        data=df,family='binomial')
m2<-glm(paste('strong_border~',controls,key_terms),
        data=df,family='binomial')
m3<-glm(paste('law_protecting_culture~',controls,key_terms),
        data=df,family='binomial')
m4<-glm(paste('no_money_for_refugee_integration~',controls,key_terms),
        data=df,family='binomial')
m5<-glm(paste('worry_undocumented~',controls,key_terms),
        data=df,family='binomial')
m6<-glm(paste('worry_culture~',controls,key_terms),
        data=df,family='binomial')

stargazer(m1,m2,m3,m4,m5,m6,omit = 'region',column.separate = c(1,1,1,1,1,1),type='text',out = 'table3.txt',
          covariate.labels = c('close to border','highschool','precarious','male','treatment','voteright','treatment:right'),
          dep.var.labels =  c('No Refugees','L: Border','L: Culture','L: Money','W: Undoc','W: Culture'),
          digits=2,column.sep.width = "2pt",dep.var.caption='Dependent Variables: Respondent Anti-Refugee Response',notes.append = T,notes = 'Region FE')


#interaction plots, of Figure 4

color<- c('#ADADAD','#1E1E1E')
p2<-cat_plot(m2, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Support stronger border)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M2", x = .8, y = 1.05, color = "black",size=7)

p1<-cat_plot(m1, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Accept no refugees at all)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M1", x = .8, y = 1.05, color = "black",size=7)

p5<-cat_plot(m5, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Worry about undocumented)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M5", x = .8, y = 1.05, color = "black",size=7)

p6<-cat_plot(m6, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Worry about diff. culture)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M6", x = .8, y = 1.05, color = "black",size=7)

p3<-cat_plot(m3, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Support law protecting HU culture)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M3", x = .8, y = 1.05, color = "black",size=7)

p4<-cat_plot(m4, pred = voteright, modx = treatment,int.width = .95 ,y.label = 'P(Against spending on refugees)',robust = T,color.class=color,
             line.thickness=1.5,errorbar.width = .2,dodge.width=.4)+ ylim(.2,1.1)+scale_x_discrete(labels=c("0"='Non-right','1'='Right'))+theme(plot.margin = unit(c(.75,1.5,.75,1.5), "lines"))+
  theme(legend.position = "none",text = element_text(size=18), axis.title.x=element_text(size=0),axis.title.y=element_text(size=18),axis.text.x = element_text(size=18))+
  annotate("text", label = "M4", x = .8, y = 1.05, color = "black",size=7)

gridplot<-ggarrange(p1,p2,p3,p4,p5,p6, nrow = 2,ncol = 3, common.legend = TRUE, legend="right")
ggsave('Figure4.pdf',gridplot,scale = 2)



#Validity of treatment using previous encounters with refugees (see Appendix)
m0<-glm(met_refugee_prev_12_months~1, data=df,family='binomial')
m1<-glm(met_refugee_prev_12_months~treatment , data=df,family='binomial')
m2<-glm(met_refugee_prev_12_months~treatment +personally_know_immigrant_or_ref, data=df,family='binomial')
m3<-glm(met_refugee_prev_12_months~treatment +personally_know_immigrant_or_ref+within_25km_of_border, data=df,family='binomial')
stargazer(m0,m1,m2,m3,type='text',dep.var.labels = 'Respondent has encountered refugee in previous 12 months',out='AppendixTable.txt',
          covariate.labels = c('Treatment','Respondent knows foreigner','Within 25km of border'))

